<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>订单型号完整性检查</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__CDN__/assets/css/backend{$Think.config.app_debug?'':'.min'}.css?v={$Think.config.site.version}">
    <script src="__CDN__/assets/js/jquery.min.js"></script>
    <script src="__CDN__/assets/libs/bootstrap/dist/js/bootstrap.min.js"></script>
    <script src="__CDN__/assets/libs/fastadmin-layer/dist/layer.js"></script>
    <script src="__CDN__/assets/libs/toastr/toastr.js"></script>
</head>
<body>
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">订单型号完整性检查</h3>
            </div>
            <div class="panel-body">
                <div class="row">
                    <div class="col-md-12">
                        <button type="button" class="btn btn-primary" id="check-btn">开始检查</button>
                        <button type="button" class="btn btn-success" id="refresh-btn" style="display:none;">刷新结果</button>
                    </div>
                </div>
                
                <div class="row" style="margin-top: 20px;">
                    <div class="col-md-12">
                        <div id="result-container" style="display:none;">
                            <h4>检查结果</h4>
                            <div id="result-content"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 修复模态框 -->
    <div class="modal fade" id="fix-modal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title">修复订单型号</h4>
                </div>
                <div class="modal-body">
                    <form id="fix-form">
                        <input type="hidden" id="fix-order-id" name="order_id">
                        <div class="form-group">
                            <label>订单号</label>
                            <input type="text" class="form-control" id="fix-order-no" readonly>
                        </div>
                        <div class="form-group">
                            <label>客户名称</label>
                            <input type="text" class="form-control" id="fix-customer-name" readonly>
                        </div>
                        <div class="form-group">
                            <label>产品型号 <span class="text-danger">*</span></label>
                            <select class="form-control" id="fix-model-id" name="model_id" required>
                                <option value="">请选择型号</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label>数量 <span class="text-danger">*</span></label>
                            <input type="number" class="form-control" id="fix-quantity" name="quantity" min="1" required>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="fix-submit-btn">确定修复</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        $(function() {
            // 加载型号列表
            function loadModels() {
                $.get('scanwork/productmodel/index', function(data) {
                    if (data && data.rows) {
                        var modelSelect = $('#fix-model-id');
                        modelSelect.empty().append('<option value="">请选择型号</option>');
                        data.rows.forEach(function(model) {
                            var displayName = model.product ? (model.product.name + ' - ' + model.name) : model.name;
                            if (model.model_code) {
                                displayName += ' (' + model.model_code + ')';
                            }
                            modelSelect.append('<option value="' + model.id + '">' + displayName + '</option>');
                        });
                    }
                });
            }
            
            // 开始检查
            $('#check-btn').click(function() {
                $(this).prop('disabled', true).text('检查中...');
                
                $.post('scanwork/allocation/checkOrderModels', function(data) {
                    $('#check-btn').prop('disabled', false).text('开始检查');
                    
                    if (data.code === 1) {
                        displayResults(data.data);
                        $('#refresh-btn').show();
                    } else {
                        Layer.alert(data.msg || '检查失败');
                    }
                }).fail(function() {
                    $('#check-btn').prop('disabled', false).text('开始检查');
                    Layer.alert('网络错误，请重试');
                });
            });
            
            // 刷新结果
            $('#refresh-btn').click(function() {
                $('#check-btn').click();
            });
            
            // 显示结果
            function displayResults(orders) {
                var html = '';
                if (orders.length === 0) {
                    html = '<div class="alert alert-success">所有订单都有完整的型号信息！</div>';
                } else {
                    html = '<div class="alert alert-warning">发现 ' + orders.length + ' 个订单缺少型号信息：</div>';
                    html += '<table class="table table-bordered table-hover">';
                    html += '<thead><tr><th>订单号</th><th>客户名称</th><th>总数量</th><th>状态</th><th>操作</th></tr></thead>';
                    html += '<tbody>';
                    
                    orders.forEach(function(order) {
                        html += '<tr>';
                        html += '<td>' + order.order_no + '</td>';
                        html += '<td>' + order.customer_name + '</td>';
                        html += '<td>' + order.total_quantity + '</td>';
                        html += '<td>' + order.status_text + '</td>';
                        html += '<td><button type="button" class="btn btn-sm btn-primary fix-btn" data-order=\'' + JSON.stringify(order) + '\'>修复</button></td>';
                        html += '</tr>';
                    });
                    
                    html += '</tbody></table>';
                }
                
                $('#result-content').html(html);
                $('#result-container').show();
            }
            
            // 修复按钮点击
            $(document).on('click', '.fix-btn', function() {
                var order = $(this).data('order');
                $('#fix-order-id').val(order.id);
                $('#fix-order-no').val(order.order_no);
                $('#fix-customer-name').val(order.customer_name);
                $('#fix-model-id').val('');
                $('#fix-quantity').val('');
                $('#fix-modal').modal('show');
            });
            
            // 修复提交
            $('#fix-submit-btn').click(function() {
                var formData = $('#fix-form').serialize();
                
                if (!$('#fix-model-id').val() || !$('#fix-quantity').val()) {
                    Layer.alert('请填写完整信息');
                    return;
                }
                
                $(this).prop('disabled', true).text('修复中...');
                
                $.post('scanwork/allocation/fixOrderModels', formData, function(data) {
                    $('#fix-submit-btn').prop('disabled', false).text('确定修复');
                    
                    if (data.code === 1) {
                        Layer.msg(data.msg, {icon: 1});
                        $('#fix-modal').modal('hide');
                        // 刷新结果
                        $('#check-btn').click();
                    } else {
                        Layer.alert(data.msg || '修复失败');
                    }
                }).fail(function() {
                    $('#fix-submit-btn').prop('disabled', false).text('确定修复');
                    Layer.alert('网络错误，请重试');
                });
            });
            
            // 页面加载时加载型号列表
            loadModels();
        });
    </script>
</body>
</html>
